package com.jqkb.service.util;

import android.content.Intent;
import android.os.Handler;

import android.widget.Toast;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.jqkb.TheApplication;
import com.jqkb.pages.LoginActivity;
import com.jqkb.service.NewMessageService;
import com.jqkb.service.bean.User;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.POST;

import java.io.IOException;
import java.util.List;

public class Converter {
    static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    public static <T> void convert(Class<T> beanClazz, Call<ResponseBody> call, CallBack<T> callBack) {
        convert(beanClazz, call, callBack, "data");
    }

    public static <T> void convert(final Class<T> beanClazz, Call<ResponseBody> call, final CallBack<T> callBack, final String path) {
        final Handler handler = new Handler();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(final Call<ResponseBody> call, Response<ResponseBody> response) {
                final JsonNode responseNode;
                try {
                    String responseText = response.body().string();
                    responseNode = objectMapper.readTree(responseText);
                    JsonNode dataNode = responseNode;
                    if (path != null) {
                        dataNode = responseNode.path(path);
                    }
                    final int code = responseNode.path("code").asInt();
                    final boolean success = responseNode.path("success").asBoolean();
                    final String message = responseNode.path("message").asText();
                    if (success) {
                        final T convertValue;
                        if (callBack instanceof ListCallBack) {
                            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, beanClazz);
                            convertValue = objectMapper.convertValue(dataNode, javaType);
                        } else if (callBack instanceof BeanCallBack) {
                            convertValue = objectMapper.convertValue(dataNode, beanClazz);
                        } else {
                            convertValue = null;
                        }

                        if (callBack != null) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callBack.onSuccess(convertValue, responseNode);
                                }
                            });
                        }
                    } else {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (code == 1001) {//需要登录
                                    Toast.makeText(TheApplication.instance, message, Toast.LENGTH_SHORT).show();
                                    handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            TheApplication.getSharedPreferences().edit().remove("user").commit();
                                            TheApplication.setCurrentUser(null);

                                            TheApplication.instance.stopService(new Intent(TheApplication.instance, NewMessageService.class));

                                            Intent intent = new Intent(TheApplication.instance, LoginActivity.class);
                                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                            TheApplication.instance.startActivity(intent);
                                        }
                                    }, 1000);

                                } else {
                                    callBack.onFail(success, code, message, responseNode);
                                }
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onNetworkFail(call);
                        }
                    });
                }
            }

            @Override
            public void onFailure(final Call<ResponseBody> call, Throwable t) {
                try {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onNetworkFail(call);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }


}
